﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Customer_Profile.eCard.DBDao;
using KVB.Common;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard
{
    public partial class CpartyEdit : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        string cparty_id = "";
        public string strScript = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request["cparty_id"] != null)
            {
                cparty_id = Request["cparty_id"].ToString();
            }

            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }

            if (!Page.IsPostBack)
            {
                Comm.BindDll(ref dropIDtype, "IDType", false, "");
                bindCpartyInfo(cparty_id);
            }
        }

        private void bindCpartyInfo(string s_cparty_id)
        {
            try
            {
                DataSet ds = CpartyDao.SearchCpartybyCpartyID(cparty_id);
                txtAddress.Value = ds.Tables[0].Rows[0]["address"].ToString();
                txtBirthday.Value = ds.Tables[0].Rows[0]["birthday"].ToString();
                txtEmail.Value = ds.Tables[0].Rows[0]["email"].ToString();
                txtIDcode.Value = ds.Tables[0].Rows[0]["id_no"].ToString();
                txtName.Value = ds.Tables[0].Rows[0]["cparty_name"].ToString();
                txtPhone.Value = ds.Tables[0].Rows[0]["phone"].ToString();
                txtZip.Value = ds.Tables[0].Rows[0]["zipcode"].ToString();
                dropIDtype.Items.FindByValue(ds.Tables[0].Rows[0]["id_type"].ToString().Trim()).Selected = true;
                dropSex.Items.FindByValue(ds.Tables[0].Rows[0]["sex"].ToString().Trim()).Selected = true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnCpartySave_Click(object sender, EventArgs e)
        {
            try
            {
                string cparty_name = txtName.Value;
                string phone = txtPhone.Value;
                string id_type = dropIDtype.SelectedItem.Value;
                string id_no = txtIDcode.Value;
                string sex = dropSex.SelectedItem.Value;
                string birthday = txtBirthday.Value;
                string email = txtEmail.Value;
                string zipcode = txtZip.Value;
                string address = txtAddress.Value;
                string entity = strEntity;  //dropEntity.SelectedItem.Value;
                CpartyDao.EditCparty(cparty_id,cparty_name, phone, id_type, id_no, sex, birthday, email, zipcode, address);
                strScript = "<script>alert('更新成功！'); window.opener.refresh();window.opener=null;window.close();</script>";
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("btnCpartySave_Click", ex.ToString());
                Response.Write("修改失败！请联系管理员<br>");
                Response.Write(ex.ToString());
            }
        }
    }
}